package com.example.testwebrtc;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class CallActivity extends AppCompatActivity {

    private TextView mLogcatView;

    private EditText editText;
    private Button sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);

        mLogcatView = findViewById(R.id.LogcatView);
        editText=findViewById(R.id.etText);
        sendBtn=findViewById(R.id.sendBtn);

        SignalClient.getInstance().setSignalEventListener(mOnSignalEventListener);

        //获取用户输入
        String serverAddr = getIntent().getStringExtra("ServerAddr");
        String roomName = getIntent().getStringExtra("RoomName");
        SignalClient.getInstance().joinRoom(serverAddr, roomName);

        //发送消息
        sendBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String message=editText.getText().toString();
                SignalClient.getInstance().getmSocket().emit("message",roomName,message);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doLeave();
    }

    public void doLeave() {
        logcatOnUI("Leave room, Wait ...");
        SignalClient.getInstance().leaveRoom();
    }

    private SignalClient.OnSignalEventListener
            mOnSignalEventListener = new SignalClient.OnSignalEventListener() {

        @Override
        public void onConnected() {
            logcatOnUI("Signal Server Connected !");
        }

        @Override
        public void onConnecting() {
            logcatOnUI("Signal Server Connecting !");
        }

        @Override
        public void onDisconnected() {
            logcatOnUI("Signal Server Disconnected!");
        }

        @Override
        public void onUserJoined(String roomName, String userID){
            logcatOnUI("local user joined!");
        }

        @Override
        public void onUserLeaved(String roomName, String userID){
            logcatOnUI("local user leaved!");
        }

        @Override
        public void onRemoteUserJoined(String roomName) {
            logcatOnUI("Remote User Joined, room: " + roomName);
            //进行媒体协商
        }

        @Override
        public void onRemoteUserLeaved(String roomName, String userID) {
            logcatOnUI("Remote User Leaved, room: " + roomName + "uid:"  + userID);
        }

        @Override
        public void onRoomFull(String roomName, String userID){
            logcatOnUI("The Room is Full, room: " + roomName + "uid:"  + userID);

            finish();
        }

        @Override
        public void onMessage(String message) {
//            Log.i("zcwfeng", "onMessage: " + message);
            logcatOnUI("onMessage:" + message);
        }


    };

    private void logcatOnUI(String msg) {
        Log.i("zcwfeng", msg);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String output = mLogcatView.getText() + "\n" + msg;
                mLogcatView.setText(output);
            }
        });
    }
}
